package com.simple.car.loader.server.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.simple.car.loader.server.R;
import com.simple.car.loader.server.dao.RunningInfoDao;
import com.simple.car.loader.server.listener.TaskClickListener;
import com.simple.car.loader.server.manager.impl.BeamManager;
import com.simple.car.loader.server.service.LinkerService;
import com.simple.car.loader.server.service.RunningLocationService;
import com.simple.car.loader.server.tools.RunningHandler;
import com.simple.car.loader.server.tools.Server;
import com.simple.car.loader.server.utils.IPUtil;
import com.simple.car.loader.server.utils.PermissionUtil;

public class MainActivity extends AppCompatActivity {

    private TextView ip, port, record, waiting, processing, done;

    private LinearLayout eventLayout;

    private GestureDetector gestureDetector;

    private TaskClickListener taskClickListener;

    private TaskClickListener.RecorderListener recorderListener = count -> {
        RunningInfoDao runningInfoDao = (RunningInfoDao) BeamManager.getInstance().lookup(RunningInfoDao.class.getName());
        long number = runningInfoDao.count();
        record.setText(String.valueOf(number));
        // 任务分发事件
        RunningHandler runningHandler = (RunningHandler) BeamManager.getInstance().lookup(RunningHandler.class.getName());
        runningHandler.handle();
    };

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        taskClickListener = (TaskClickListener) BeamManager.getInstance().lookup(TaskClickListener.class.getName());
        gestureDetector = new GestureDetector(this, ((TaskClickListener) BeamManager.getInstance().lookup(TaskClickListener.class.getName())));
        ip = findViewById(R.id.ip);
        port = findViewById(R.id.port);
        eventLayout = findViewById(R.id.event_layout);
        record = findViewById(R.id.record);
        waiting = findViewById(R.id.waiting);
        processing = findViewById(R.id.processing);
        done = findViewById(R.id.done);
        eventLayout.setOnTouchListener((v, event) -> gestureDetector.onTouchEvent(event));
        String ip = IPUtil.getIP(this);
        this.ip.setText(ip);
        port.setText(String.valueOf(Server.PORT));
        PermissionUtil.isStoragePermissionGranted(this);
        startService(new Intent(this, LinkerService.class));
        startService(new Intent(this, RunningLocationService.class));
        RunningHandler runningHandler = (RunningHandler) BeamManager.getInstance().lookup(RunningHandler.class.getName());
        runningHandler.setWaitingListener(count -> waiting.setText(String.valueOf(count)));
        runningHandler.setProcessingListener(count -> processing.setText(String.valueOf(count)));
        runningHandler.setDoneListener(count -> done.setText(String.valueOf(count)));
    }
}